<?php
/**
 * @title 5532. 奇偶树 - 层次遍历
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param TreeNode $root
     * @return Boolean
     */
    function isEvenOddTree($root) {
        if($root === null){
            return true;
        } else {}

        /**
         * @since 2020-10-04 偶数标志
         */
        $even = true;

        /**
         * @since 2020-10-10 节点最大值最小值
         */
        $max = pow(10, 6)+1;
        $min = 0;

        /**
         * @since 2020-10-04 层次遍历
         */
        $nexts = [$root];
        while($nexts){
            $nextTmp = [];

            /**
             * @since 2020-10-04 是否偶数层
             */
            if($even){
                /**
                 * @since 2020-10-04 上个数
                 */
                $lastNum = $min;

                /**
                 * @since 2020-10-10 遍历当前层节点
                 */
                foreach ($nexts as $node){
                    /**
                     * @since 2020-10-04 偶数层, 是否奇数
                     */
                    $val = $node->val;
                    if($val%2 == 1){
                        /**
                         * @since 2020-10-04 是否递增
                         */
                        if($val > $lastNum){
                            $lastNum = $val;
                        } else {
                            return false;
                            break;
                        }
                    } else {
                        return false;
                        break;
                    }

                    /**
                     * @since 2020-10-04 左右子树
                     */
                    if($node->left !== null){
                        $nextTmp[] = $node->left;
                    } else {}
                    if($node->right !== null){
                        $nextTmp[] = $node->right;
                    } else {}
                }
            } else {
                /**
                 * @since 2020-10-04 上个数
                 */
                $lastNum = $max;

                /**
                 * @since 2020-10-10 遍历当前层节点
                 */
                foreach ($nexts as $node){
                    /**
                     * @since 2020-10-04 奇数层, 是否偶数
                     */
                    $val = $node->val;
                    // echo "val={$val}", PHP_EOL;
                    if($val%2 == 0){
                        /**
                         * @since 2020-10-04 是否递减
                         */
                        if($val < $lastNum){
                            $lastNum = $val;
                        } else {
                            return false;
                            break;
                        }
                    } else {
                        return false;
                        break;
                    }

                    /**
                     * @since 2020-10-04 左右子树
                     */
                    if($node->left !== null){
                        $nextTmp[] = $node->left;
                    } else {}
                    if($node->right !== null){
                        $nextTmp[] = $node->right;
                    } else {}
                }
            }

            /**
             * @since 2020-10-04 下次迭代
             */
            $even = !$even;
            $nexts = $nextTmp;
        }

        /**
         * @return
         */
        return true;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.04_2.php
 */
$datas = [
    [1,10,4,3,null,7,9,12,8,6,null,null,2],
    [5,4,2,3,3,7],
    [5,9,1,3,5,7],
    [1],
    [11,8,6,1,3,9,11,30,20,18,16,12,10,4,2,17],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    // $result = $obj->(...$data);
    $result = $obj->isEvenOddTree($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
